#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMovie>
#include <QPixmap>
#include <QPalette>
#include <QDebug>
#include "llbjm.h"
#include <llbzc.h>
#include "shangjia.h"

QSqlDatabase db;
void sqlite_Init1()
{
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("user.db");
    if(!db.open())
    {
        qDebug() << "open error";
    }
    // Create table if not exists
    QString createsql = QString("CREATE TABLE IF NOT EXISTS user("
                                "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                                "username NTEXT UNIQUE NOT NULL,"
                                "password NTEXT NOT NULL)");
    QSqlQuery query;
    if(!query.exec(createsql))
    {
        qDebug() << "user table create 失败";
    }
    else
    {
        qDebug() << "user table create 成功";
    }

    // Add balance column if it doesn't exist
    QString alterTableSql = QString("ALTER TABLE user ADD COLUMN balance REAL DEFAULT 0");
    if(!query.exec(alterTableSql))
    {
        qDebug() << "add balance column error";
    }
    else
    {
        qDebug() << "balance column added successfully";
    }
    // 创建 llbcai 表
    QString createLlbcaiTableSql = QString("CREATE TABLE IF NOT EXISTS llbcai("

                                           "username NTEXT NOT NULL,"
                                           "name NTEXT NOT NULL,"
                                           "quantity INTEGER NOT NULL,"
                                           "price REAL NOT NULL)");
    if(!query.exec(createLlbcaiTableSql))
    {
        qDebug() << "llbcai table create 失败";
    }
    else
    {
        qDebug() << "llbcai table create 成功";
    }

}


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    sqlite_Init1();

    // ui->label_3->setFixedSize(400,200);
    // ui->label_3->move(0,0);
    QMovie *movie = new QMovie(":/Image/mario.gif");
    ui->label_3->setMovie(movie);
    movie->start();

    // 设置按钮的图标
    // ui->pushButton_2->setIcon(QIcon(":/Image/LuffyQ.png")); // 使用相对路径或绝对路径

    // // 设置按钮的文本
    // ui->pushButton_2->setText("Click Me");

    connect(ui->pushButton, &QPushButton::clicked, this,&MainWindow::on_btn_signin_clicked);
    // connect(ui->pushButton, &QPushButton::clicked, this,
    //         &MainWindow::getLineEditText
    //         );

    qDebug()<<ui->lineEdit->text();
}

void MainWindow::on_btn_signin_clicked()
{
    //sqlite_Init1();//初始化数据库
    QString username = ui->lineEdit->text();
    QString password = ui->lineEdit_2->text();
    //sql查询语句
    QString sql=QString("select * from user where username='%1' and password='%2'")
                      .arg(username).arg(password);
    //创建执行语句对象
    QSqlQuery query(sql,db);
    //判断执行结果
    if(!query.next())
    {
        qDebug()<<"Login error";
        QMessageBox::information(this,"登录认证","登录失败,账户或者密码错误");
    }
    else
    {
        qDebug()<<"Login success";
        QMessageBox::information(this,"登录认证","登录成功");
        //登录成功后可以跳转到其他页面
        llbjm *w = new llbjm;
        w->string = getLineEditText();
        qDebug()<<w->string;

        w->show();
        this->close();
        // 调用 getLineEditText() 并打印其返回值
        qDebug() << "Current line edit text:" << getLineEditText();

    }
}
QString MainWindow::getLineEditText()
{

    QString username = ui->lineEdit->text();

    return username;
}


MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_3_clicked()
{
    this->close();
    llbzc *s = new llbzc;
    s->show();
}


void MainWindow::on_pushButton_2_clicked()
{
    shangjia * shang = new shangjia;
    shang->show();

}

